package com.springboot.cloud.demos.producer.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.demos.entity.form.MemberCardForm;
import com.springboot.cloud.demos.entity.form.MemberCardQueryForm;
import com.springboot.cloud.demos.entity.param.MemberCardQueryParam;
import com.springboot.cloud.demos.entity.po.MemberCard;
import com.springboot.cloud.demos.producer.service.IMemberCardService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/memberCard")
@Api("memberCard")
@Slf4j
public class MemberCardController {

    @Autowired
    private IMemberCardService memberCardService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "memberCardForm", value = "新增产品form表单", required = true, dataType = "MemberCardForm")
    @PostMapping
    public Result add(@Valid @RequestBody MemberCardForm memberCardForm) {
        log.info("name:{}", memberCardForm);
        MemberCard memberCard = memberCardForm.toPo(MemberCard.class);
        return Result.success(memberCardService.add(memberCard));
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(memberCardService.delete(id));
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "memberCardForm", value = "产品实体", required = true, dataType = "MemberCardForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody MemberCardForm memberCardForm) {
        MemberCard memberCard = memberCardForm.toPo(MemberCard.class);
        memberCard.setId(id);
        return Result.success(memberCardService.update(memberCard));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result<MemberCard> get(@PathVariable String id) {
        log.info("get with id:{}", id);
        MemberCard memberCard =memberCardService.get(id);
        memberCardService.update(memberCard);
        return Result.success(memberCard);
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "name", value = "产品名称", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping
    public Result query() {
        MemberCardQueryParam memberCardQueryParam = new MemberCardQueryParam();
        return Result.success(memberCardService.query(memberCardQueryParam));
    }

    @ApiOperation(value = "搜索产品", notes = "根据条件查询产品信息")
    @ApiImplicitParam(name = "memberCardQueryForm", value = "产品查询参数", required = true, dataType = "MemberCardQueryForm")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @PostMapping(value = "/conditions")
    public Result search(@Valid @RequestBody MemberCardQueryForm memberCardQueryForm) {
        log.info("search with memberCardQueryForm:{}", memberCardQueryForm);
        return Result.success(memberCardService.query(memberCardQueryForm.toParam(MemberCardQueryParam.class)));
    }
}

